package com.practice.service.admin.impl;

import com.practice.common.constant.MessageConstant;
import com.practice.common.result.Result;
import com.practice.common.utils.JwtUtil;
import com.practice.mapper.admin.LoginMapper;
import com.practice.pojo.admin.dto.AdminLoginDTO;
import com.practice.pojo.admin.entity.Login;
import com.practice.pojo.admin.vo.AdminLoginVO;
import com.practice.service.admin.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public Result login(AdminLoginDTO adminLoginDTO) {
        //拿到账户密码
        String username = adminLoginDTO.getLogin_name();
        String password = adminLoginDTO.getPassword();
        //查询数据库数据,并查询账户是否存在
        Login login = loginMapper.getByusername(username);
        if (username == null){
            //账户异常
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //获取加密密码
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());

        //密码比较
        if (!pwd.equals(login.getPassword())){
            //密码错误
            return Result.error(MessageConstant.PASSWORD_ERROR);
        }

        String token = JwtUtil.getToken(login.getId());

        Map map = new HashMap();
        map.put("token",token);

        return Result.success(map);
    }
}
